package org.example.sudoku;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class Sudoku extends Activity {

	private final String TAG = "Sudoku";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sudoku);

		View aboutButton = findViewById(R.id.about_button);
		aboutButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.about_button:
					Intent i = new Intent(Sudoku.this, about.class);
					startActivity(i);
					break;

				default:
					break;
				}

			}
		});

		View newGameButton = findViewById(R.id.new_button);
		newGameButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.new_button:
					openNewGameDialog();
					break;

				default:
					break;
				}

			}
		});

		View exitButton = findViewById(R.id.exit_button);
		exitButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.exit_button:
					finish();
					break;

				default:
					break;
				}

			}
		});

		View continueButton = findViewById(R.id.exit_button);
		continueButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.continue_button:
					startGame(Game.DIFFICULTY_CONTINUE);
					break;

				default:
					break;
				}

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			Intent i = new Intent(Sudoku.this, Preference.class);
			startActivity(i);
			break;

		default:
			break;
		}
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

	private void openNewGameDialog() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.new_game_title)
				.setItems(R.array.difficulty,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								startGame(which);
							}
						}).show();
	}

	private void startGame(int i) {
		Log.d(TAG, "clicked on " + i);
		Intent intent = new Intent(Sudoku.this, Game.class);
		intent.putExtra(Game.KEY_DIFFICULTY, i);
		startActivity(intent);
	}

	@Override
	protected void onResume() {
		super.onResume();
		Music.play(this, R.raw.main);
	}

	@Override
	protected void onPause() {
		super.onPause();
		Music.stop(this);
	}

}
